package com.jiangxiaofei.citycloud.activity.fleamarket;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.user.LoginAcitivity;
import com.jiangxiaofei.citycloud.adapter.FleaGoodAdapter;
import com.jiangxiaofei.citycloud.adapter.FleaMenuRecyAdapter;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.BannerBean;
import com.jiangxiaofei.citycloud.bean.FleaGoodBean;
import com.jiangxiaofei.citycloud.bean.FleaMenuBean;
import com.jiangxiaofei.citycloud.constants.BannerId;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.mvp.FleaConstant;
import com.jiangxiaofei.citycloud.mvp.FleaPersenter;
import com.jiangxiaofei.citycloud.mvp.MainConstant;
import com.jiangxiaofei.citycloud.mvp.MainPersenter;
import com.jiangxiaofei.citycloud.util.BannerViewHolder;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.zhouwei.mzbanner.MZBannerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * @author: JiangXFei
 * 日期: 2017/10/19 0019
 * @title 物资交流主页面
 */

public class FleaMarketHomeActivity extends BaseActivity implements
        FleaConstant.GetFleaMenuView, FleaConstant.GetNewGoodsView,
        MainConstant.GetBannerView {
    FleaGoodAdapter wzGoodListAdapter;
    List<FleaGoodBean> goodListBean = new ArrayList<>();
    @InjectView(R.id.recy_goods)
    RecyclerView mRecyGoods;
    @InjectView(R.id.img_back)
    ImageView imgBack;
    @InjectView(R.id.tv_search_type)
    TextView tvSearchType;
    @InjectView(R.id.et_search)
    TextView etSearch;
    @InjectView(R.id.img_publish)
    TextView imgPublish;
    @InjectView(R.id.banner)
    MZBannerView bannerView;
    @InjectView(R.id.recy_flea_home_menu)
    RecyclerView recyFleaHomeMenu;
    @InjectView(R.id.srf_layout)
    SmartRefreshLayout refreshLayout;
    @InjectView(R.id.tv_have)
    TextView tvHave;
    @InjectView(R.id.tv_need)
    TextView tvNeed;
    @InjectView(R.id.linear_search)
    LinearLayout linearSearch;
    private List<Integer> imgsList;
    private List<FleaMenuBean> menuBeanList = new ArrayList<>();
    private FleaConstant.GetFleanMenuPersenter menuPersenter;
    private FleaMenuRecyAdapter fleaMenuRecyAdapter;
    private PopupWindow mPopupWindowPublish;
    private View popupView;
    private int page = 1;
    private FleaConstant.GetNewGoodsPersenter getNewGoodsPersenter;
    private MainConstant.GetBannerPersenter getBannerPersenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_flea_market_layout);
        ButterKnife.inject(this);
        mContext = this;
        mActivity = this;
        initBannerDate();
        initAdapter();
        initMenuRecy();
        initPopupPublish();
        initrfrLayout();
        menuPersenter = new FleaPersenter(mContext, (FleaConstant.GetFleaMenuView) mActivity);
        menuPersenter.getMainFleaMenu();
        getNewGoodsPersenter = new FleaPersenter(mContext, (FleaConstant.GetNewGoodsView) mActivity);
        getNewGoodsPersenter.getNewGoods();
        getBannerPersenter = new MainPersenter(mContext, (MainConstant.GetBannerView) mActivity);
        getBannerPersenter.getBanner();
    }

    private void initrfrLayout() {
        refreshLayout.setRefreshHeader(new MaterialHeader(this).setShowBezierWave(true));
//        .setPrimaryColors(0xff66caff);
        refreshLayout.setRefreshFooter(new BallPulseFooter(this).setSpinnerStyle(SpinnerStyle.Scale));

        refreshLayout.setOnLoadmoreListener(refreshlayout -> {
            refreshlayout.finishLoadmore(2000);
            page++;
            getNewGoodsPersenter.getNewGoods();
        });
        refreshLayout.setOnRefreshListener(refreshlayout -> {
            refreshlayout.finishRefresh(2000);
            page = 1;
            goodListBean.clear();
            getNewGoodsPersenter.getNewGoods();
        });
    }

    /**
     * 初始化菜单recy
     */
    private void initMenuRecy() {
        fleaMenuRecyAdapter = new FleaMenuRecyAdapter(R.layout.item_flea_recy_home_menu, menuBeanList);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 5);
        recyFleaHomeMenu.setLayoutManager(gridLayoutManager);
        fleaMenuRecyAdapter.setOnItemClickListener((adapter, view, position) -> {
            Intent intent = new Intent(mContext, FleaMarketHaveActivity.class);
            intent.putExtra(StringConstants.FLEABANNERTYPE, position);
            intent.putExtra(FleaMarketHaveActivity.HAVETYPEID, Integer.valueOf(menuBeanList.get(position).getId()));
            startActivity(intent);
        });
        recyFleaHomeMenu.setAdapter(fleaMenuRecyAdapter);
    }


    /**
     * 初始化BANNER数据
     */
    private void initBannerDate() {
        imgsList = new ArrayList<>();
        imgsList.add(R.drawable.ic_wz_banner);
    }


    @Override
    protected void setListener() {

    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

    }

    /**
     * 初始化Adapter
     */
    private void initAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        mRecyGoods.setLayoutManager(gridLayoutManager);
        wzGoodListAdapter = new FleaGoodAdapter(R.layout.item_flea_good, goodListBean);
        wzGoodListAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Intent intent = new Intent(mContext, FleaMarketGoodsDetailActivity.class);
                    intent.putExtra(StringConstants.FLEAGOODID, Integer.valueOf(goodListBean.get(position).getId()));
                    startActivity(intent);
                }
        );
        mRecyGoods.setAdapter(wzGoodListAdapter);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.img_back, R.id.img_publish, R.id.tv_need, R.id.linear_search})
    public void bOnClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.img_back:
                //返回
                finish();
                break;
            case R.id.img_publish:
                //发布
                if (EmptyUtils.isEmpty(Constants.userInfo)) {
                    Toast.makeText(mContext, "请先登陆！", Toast.LENGTH_SHORT).show();
                    startActivity(new Intent(this, LoginAcitivity.class));
                } else {
                    showPopup();
                }
                break;
            case R.id.tv_need:
                startActivity(new Intent(mActivity, FleaMarketNeedActivity.class));
                break;
            case R.id.linear_search:
                startActivity(new Intent(mActivity, FleaMarketSearchActivity.class));
                break;
        }
    }


    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getFleaMenu() {
        return 0;
    }

    @Override
    public void getFleaMainMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {
        menuBeanList.clear();
        menuBeanList.addAll(fleaMenuBeans);
        fleaMenuRecyAdapter.notifyDataSetChanged();
    }

    @Override
    public void getFleaSencendMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {

    }

    @Override
    public void getFleaThreeMenuOnSuccess(List<FleaMenuBean> fleaMenuBeans) {

    }

    /**
     * 初始化发布popupWindow
     */
    private void initPopupPublish() {
        mPopupWindowPublish = new PopupWindow(mContext);
        popupView = LayoutInflater.from(mContext).inflate(R.layout.popup_ios_buttom, null);
        showAnimation(popupView);
        mPopupWindowPublish.setContentView(popupView);
        mPopupWindowPublish.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindowPublish.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindowPublish.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        mPopupWindowPublish.setFocusable(true);
        mPopupWindowPublish.setOutsideTouchable(false);
        TextView tvGong = popupView.findViewById(R.id.tv_publish_gong);
        TextView tcXu = popupView.findViewById(R.id.tv_publish_xu);
        TextView tvCannel = popupView.findViewById(R.id.tv_cannel);
        tvGong.setOnClickListener(v -> {
            mPopupWindowPublish.dismiss();
            startActivity(new Intent(mContext, FleaMarketHavePublishActivity.class));
        });
        tvCannel.setOnClickListener(v -> mPopupWindowPublish.dismiss());
        tcXu.setOnClickListener(v -> {
            mPopupWindowPublish.dismiss();
            startActivity(new Intent(mContext, FleaMarketNeedPublishActivity.class));
        });
    }

    private void showPopup() {
        showAnimation(popupView);
        mPopupWindowPublish.showAtLocation(getCurrentFocus(), Gravity.BOTTOM, 0, 0); // 设置背景颜色变暗
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = 0.7f;
        mActivity.getWindow().setAttributes(lp);
        mPopupWindowPublish.setOnDismissListener(() -> {
            WindowManager.LayoutParams lp1 = mActivity.getWindow().getAttributes();
            lp1.alpha = 1f;
            mActivity.getWindow().setAttributes(lp1);
        });
    }

    private void showAnimation(View popView) {
        AnimationSet animationSet = new AnimationSet(false);
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.0f);
        alphaAnimation.setDuration(300);
        TranslateAnimation translateAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f,
                Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f
        );
        translateAnimation.setDuration(300);
        animationSet.addAnimation(alphaAnimation);
        animationSet.addAnimation(translateAnimation);
        popView.startAnimation(animationSet);
    }

    @Override
    public int getNewGoodsPage() {
        return page;
    }

    @Override
    public void getNewGoodsSuccess(List<FleaGoodBean> goodBeanList) {
        if (goodBeanList.size() > 0) {
            goodListBean.addAll(goodBeanList);
            wzGoodListAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getBannerParam() {
        return BannerId.FLEAHOMEID;
    }

    @Override
    public void bannerSuccess(List<BannerBean> bannerBeans) {
        bannerView.setPages(bannerBeans, new BannerViewHolder(mContext));
        bannerView.start();
    }
}
